<?php


namespace app\common\business;


use think\Exception;
use think\facade\Cache;

class Goods extends BusBase
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new \app\common\model\mysql\Goods();
    }

    public function insertData($data)
    {
        $this->model->startTrans();

        try {
            $data['production_time'] = strtotime($data['production_time']);
            $goods_id = $this->add($data);
            if (!$goods_id) {
                return false;
            }

            //单规格
            if ($data['goods_specs_type'] == 1) {
                $goods_sku_data = [
                    'goods_id' => $goods_id,
                ];

                return true;
            }


            //多规格
            $data['goods_id'] = $goods_id;
            //插入sku的数据,并获得插入数据
            $res = (new GoodsSku())->saveAll($data);
            if (empty($res)) {
                throw  new Exception('sku主表更新失败');
            }

            //总库存
            $stock = array_sum(array_column($res, 'stock'));
            $goods_update_data = [
                'price' => $res[0]['price'],
                'cost_price' => $res[0]['cost_price'],
                'stock' => $stock,
                'sku_id' => $res[0]['id'],
            ];

            $goods_rss = $this->model->updateById($goods_id, $goods_update_data);
            if (!$goods_rss) {
                throw  new Exception('goods主表更新失败');
            }

            $this->model->commit();
            return true;
        } catch (Exception $e) {
            $this->model->rollback();
            return false;
        }

        return true;
    }

    public function getNormalGoods($data)
    {
        return $this->model->getNormalGoods($data)->toArray();
    }

    public function getRotationChart()
    {
        return $this->model->getRotationChart()->toArray();
    }

    public function categoryGoodsRecommend(array $category_ids)
    {
        if (!$category_ids) {
            return [];
        }

        $data = [];

        foreach ($category_ids as $k => $category_id) {
//            $data[$k]['categorys'] = (new Category())->getCate($category_id)->toArray();
        }


        foreach ($category_ids as $key => $category_id) {
            $data[$key]['goods'] = $this->getNormalGoodsFindInsertCategoryId($category_id);
        }


        return $data;
    }

    public function getNormalGoodsFindInsertCategoryId($category_id)
    {
        $field = "sku_id as id , title ,price , recommend_image as image";

        try {
            $res = $this->model->getNormalGoodsFindInsertCategoryId($category_id, $field);
        } catch (Exception $e) {
            return [];
        }

        return $res->toArray();
    }

    public function getNormalList(array $data, $page_size)
    {
        $res = $this->model->where($data)->field("big_image as image , sku_id as id , price ,title ")->paginate($page_size)->toArray();
        return [
            'list' => $res['data'],
            'total' => $res['total'],
        ];
    }

    public function getGoodsDetailBySkuId($id)
    {
        $data = (new GoodsSku())->getGoodsDetailBySkuId($id);
        if (!$data) {
            return [];
        }

        $goods = $data['goods'];
        $sku_data = (new GoodsSku())->getSkuByGoodsId($goods['id']);

        foreach ($sku_data as $item) {
            if ($item['id'] == $id) {
                $flag_value = $item['specs_value_ids'];
            }
        }


        $gids = array_column($sku_data, 'id', 'specs_value_ids');

        if ($goods['goods_specs_type'] == 1) {
            $sku = [];
        } else {
            $sku = (new SpecsValue())->dealGoodsSkus($gids, $flag_value);
        }

        //记录数据到redis
        Cache::store('redis')->inc('mall_pv_' . $goods['id']);

        return [
            'title' => $goods['title'],
            'price' => $goods['price'],
            'cost_price' => $goods['cost_price'],
            'sales_count' => 0,
            'stock' => $data['stock'],
            'gids' => $gids,
            'image' => $goods['carousel_image'],
            'sku' => $sku,
            'detail' => [
                'd1' => [
                    '商品编码' => $data['id'],
                    '上架时间' => $goods['create_time'],
                ],
                'd2' => $goods['description'],
            ],
        ];
    }
}